﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class Report : Form
    {
        public Report()
        {
            InitializeComponent();
        }

        private void Report_Load(object sender, EventArgs e)
        {

        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            PurchaseDao p = new PurchaseDao();
            txtNumber.Text = "";
            txtTotal.Text = "";
            if (dtpFormDate.Value.AddMinutes(-5) > dtpToDate.Value)
            {
                MessageBox.Show("Từ ngày phải nhỏ hơn đến ngày");
            }
            else
            {
                DataRow row1 = p.getReport(dtpFormDate.Value, dtpToDate.Value);
                if (row1 != null)
                {
                    txtNumber.Text = (row1["NUMBER"] == null || row1["NUMBER"] == System.DBNull.Value) ? "0" : float.Parse(row1["NUMBER"].ToString()).ToString("#,###");
                    txtTotal.Text = (row1["TOTAL"] == null || row1["TOTAL"] == System.DBNull.Value) ? "0" : float.Parse(row1["TOTAL"].ToString()).ToString("#,###");
                }

                DataTable table = p.GetCustomerPurchase(dtpFormDate.Value, dtpToDate.Value);
                dtgCustomers.Rows.Clear();
                if (table != null && table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        int i = dtgCustomers.Rows.Add();
                        dtgCustomers.Rows[i].Cells[0].Value = row["ID"].ToString();
                        dtgCustomers.Rows[i].Cells[1].Value = i + 1;
                        dtgCustomers.Rows[i].Cells[2].Value = row["NAME"].ToString();
                        dtgCustomers.Rows[i].Cells[3].Value = row["NOTE"].ToString();
                        dtgCustomers.Rows[i].Cells[4].Value = DateTime.Parse(row["PURCHASE_DATE"].ToString()).ToString("dd/MM/yyyy");
                        dtgCustomers.Rows[i].Cells[5].Value = DateTime.Parse(row["CREATE_DATE"].ToString()).ToString("dd/MM/yyyy");
                        dtgCustomers.Rows[i].Cells[6].Value = double.Parse(row["TOTAL_MONTH"].ToString()).ToString("#,###.##");
                        dtgCustomers.Rows[i].Cells[7].Value = double.Parse(row["TOTAL_AMOUN"].ToString()).ToString("#,###.##"); 
                        if (i%2 ==1)
                        {
                            dtgCustomers.Rows[i].DefaultCellStyle.BackColor = Color.LightCyan;
                        }                        
                    }
                }
            }
        }

        private void txtNumber_TextChanged(object sender, EventArgs e)
        {

        }

        private void dtgCustomers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
